package com.briup.day24;

import java.io.*;

/**
 * @author ThinkBook
 */
public class Test03_Standard {
    public static void main(String[] args) throws FileNotFoundException {
      //test02();
        test03();
    }
    public static void test03() throws FileNotFoundException {
        System.out.println("main start ...");
        PrintStream ps = new PrintStream("src/file/log.txt");
        System.out.println("before 修改标准输出： ");
        System.out.println(ps);
        System.out.println("即将修改 标准输出!");
        // 改变输出的流向
        System.setOut(ps);
        System.out.println("after 修改标准输出： ");
        System.out.println(ps);
    }
    public static void test02(){
        BufferedReader br = null;
        //BufferedWriter bw = null;
        PrintStream ps = null;
        try {
            // 获取字符缓冲输入流和输出流
            FileReader fr = new FileReader("src/file/name.txt");
            FileWriter fw = new FileWriter("src/file/name02.txt");
            br = new BufferedReader(fr);
            //bw = new BufferedWriter(fw);
            ps = new PrintStream("src/file/name03.txt");
            // 完成拷贝
            String line;
            while ((line = br.readLine()) != null) {
                ps.println(line);
                //bw.write(line);
                // ready()方法，判断文件是否读取到末尾，读取末尾（流没有数据），返回false
                if (br.ready()) {
                    ps.println(line);
                }
                ps.print(line);
            }
            System.out.println("name.txt文件拷贝完成");
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (ps != null) {
                ps.close();
            }

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
